//
//  JYTLoginHandler.m
//  JYT_IOS
//
//  Created by nxa-tankeke on 16/11/17.
//  Copyright © 2016年 com.yuan. All rights reserved.
//

#import "JYTLoginHandler.h"
#import "JYTUserManager.h"
#import <SVProgressHUD.h>
#import "User.h"

@implementation JYTLoginHandler

+ (JYTLoginHandler *) shared
{
    static dispatch_once_t onceToken;
    static JYTLoginHandler *loginHandler;
    dispatch_once(&onceToken, ^{
        loginHandler = [[JYTLoginHandler alloc] init];
    });
    return loginHandler;
}

////登录
//- (void) loginWithName:(NSString *)name andPwd:(NSString *)pwd completionHandler:(void (^)(BOOL succ)) handler
//{
//    if ((name.length >= 11) && (pwd.length >= 6)) {
//        
//        JYTHttpClient *client = [JYTHttpClient sharedInstance];
//        
//        client.urlType = visitoruser;
//        
//        client.finishCallbackHandler = ^(id result, NSString *code, NSString *msg){
//            
//            NSDictionary *dic = result;
//            dic = dic[@"data"];
//            
//            //保存登录名称
//            [[JYTUserManager shared] setUserLoginName:name];
//            User *usr = [User mj_objectWithKeyValues:dic];
//            
//            if (usr) {
//                [[JYTUserManager shared] setUser:usr];
//            }
//            
//            //设置设置登录状态
//            [[JYTUserManager shared] setUsedOffLine:@"0"];
//         
//            [[JYTUserManager shared] setUserHeadUrl:dic[@"headUrl"]];
//            
//            
//            handler(YES);
//            
//            MyLog(@"\n\n%@", dic);
//            
//            //            NSNumber *n = dic[@"PageCount"];
//            //            MyLog(@"-----------==============%zd", [n integerValue]);
//        };
//        
//        client.errorCallbackHandler = ^(NSString * code, NSString * msg) {
//            //设置设置登录状态
//            [[JYTUserManager shared] setUsedOffLine:@"1"];
//            
//            handler(NO);
//        };
//        
//        NSString *md5str = [client md5_base64:pwd];
//        
//        
//        if (!md5str) {
//            return;
//        }
//        
//        NSMutableDictionary *param = [NSMutableDictionary dictionary];
//        //请求 参数
//        [param setObject:name forKey:@"UserName"];
//        [param setObject:md5str forKey:@"Md5Pwd"];
//        [param setObject:@"Android" forKey:@"PhoneMode"];
//        
//        //请求 CommandCode
//        [param setObject:@"Login" forKey:@"CommandCode"];
//        
//        [client post:@"GetDataInfo" withParam:param];
//        
//    } else {
//        //设置设置登录状态
//        [[JYTUserManager shared] setUsedOffLine:@"1"];
//      
////        if (name.length < 11) {
////            
////            [SVProgressHUD showErrorWithStatus:@"用户名错误"];
////        } else {
////            [SVProgressHUD showErrorWithStatus:@"密码错误"];
////        }
//        
//        handler(NO);
//    }
//}


- (void) loginWithName:(NSString *)name andPwd:(NSString *)pwd completionHandler:(void (^)(BOOL succ)) handler
{
    if (name && pwd) {
        
        JYTHttpClient *client = [JYTHttpClient sharedInstance];
        
        client.urlType = userCenterUrl;
        
        client.finishCallbackHandler = ^(id result, NSString *code, NSString *msg){
            
            NSDictionary *dic = result;
            dic = dic[@"data"];
            
            User *usr = [User mj_objectWithKeyValues:dic];
            
            //保存登录名称
            [[JYTUserManager shared] setUserLoginName:usr.userName];
            //保存头像地址
            [[JYTUserManager shared] setUserHeadUrl:usr.headUrl];
            //昵称
            [[JYTUserManager shared] setNikeName:usr.nickName];
            if (usr) {
                [[JYTUserManager shared] setUser:usr];
            }
            
            //设置设置登录状态
            [[JYTUserManager shared] setUsedOffLine:@"0"];
            [[JYTUserManager shared] setUserHeadUrl:dic[@"headUrl"]];
            
            MyLog(@"登陆=====\n\n%@", dic);
            
            handler(YES);
        };
        
        client.errorCallbackHandler = ^(NSString * code, NSString * msg) {
            //设置登录状态
            [[JYTUserManager shared] setUsedOffLine:@"1"];
            
            handler(NO);
        };
        
        NSString *md5str = [client md5_base64:pwd];
        
        
        if (!md5str) {
            return;
        }
        
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        //请求 参数
        [param setObject:name forKey:@"UserName"];
        [param setObject:md5str forKey:@"Md5Pwd"];
        [param setObject:@"Android" forKey:@"PhoneMode"];
        
        //请求 CommandCode
        [param setObject:@"Login" forKey:@"CommandCode"];
        
        [client post:@"GetDataInfo" withParam:param];
        
    } else {
        //设置设置登录状态
        [[JYTUserManager shared] setUsedOffLine:@"1"];
        
    
        handler(NO);
    }
}

- (void) changePwdWithUserName:(NSString *)userName andPhoneNum:(NSString *)phoneNum andPwd:(NSString *)newPwd completionHandler:(void (^)(BOOL succ)) handler
{
    JYTHttpClient *client = [JYTHttpClient sharedInstance];
    
    client.urlType = userCenterUrl;
    
    client.finishCallbackHandler = ^(id result, NSString *code, NSString *msg){
        
        handler(YES);
    };
    
    client.errorCallbackHandler = ^(NSString * code, NSString * msg) {
        handler(NO);
    };
    
    NSString *md5strn = [client md5_base64:newPwd];
    
    if (!md5strn) {
        return;
    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //请求 参数
    [param setObject:userName forKey:@"UserName"];
    [param setObject:phoneNum forKey:@"TelPhone"];
    [param setObject:md5strn forKey:@"NewPwd"];
    
    //请求 CommandCode
    [param setObject:@"SettingPwd" forKey:@"CommandCode"];
    
    [client post:@"UpdateDataInfo" withParam:param];
}

- (void) changePwd:(NSString *)oldPwd andPwd:(NSString *)newPwd andUserName:(NSString *)userName  completionHandler:(void (^)(BOOL succ)) handler
{
    if (oldPwd && newPwd) {
        
        JYTHttpClient *client = [JYTHttpClient sharedInstance];
        
        client.urlType = userCenterUrl;
        
        client.finishCallbackHandler = ^(id result, NSString *code, NSString *msg){
            
            handler(YES);
        };
        
        client.errorCallbackHandler = ^(NSString * code, NSString * msg) {
            handler(NO);
        };
        
        NSString *md5stro = [client md5_base64:oldPwd];
        
        NSString *md5strn = [client md5_base64:newPwd];
        
        
        if (!md5stro || !md5strn) {
            return;
        }
        
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        //请求 参数
        [param setObject:userName forKey:@"UserName"];
        [param setObject:md5stro forKey:@"OldPwd"];
        [param setObject:md5strn forKey:@"NewPwd"];
        
        //请求 CommandCode
        [param setObject:@"ModifyPwd" forKey:@"CommandCode"];
        
        [client post:@"UpdateDataInfo" withParam:param];
        
    } else {
        handler(NO);
    }
}

- (void) postContent:(NSString *)content  andUserName:(NSString *)userName  completionHandler:(void (^)(BOOL succ)) handler
{
    
    if (content) {
        
        JYTHttpClient *client = [JYTHttpClient sharedInstance];
        
        client.urlType = userCenterUrl;
        
        client.finishCallbackHandler = ^(id result, NSString *code, NSString *msg){
            
            handler(YES);
        };
        
        client.errorCallbackHandler = ^(NSString * code, NSString * msg) {
            handler(NO);
        };

        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        //请求 参数
        [param setObject:userName forKey:@"UserName"];
        [param setObject:content forKey:@"Content"];
    
        //请求 CommandCode
        [param setObject:@"FeedBack" forKey:@"CommandCode"];
        
        [client post:@"AddDataInfo" withParam:param];
        
    } else {
         handler(NO);
    }
}
@end
